home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
DB_CLIPP
/
2614.ZIP
/
DEFAUL.ZIP
/
DEFAULT.TXT
Wrap
Text File
|
1990-09-28
|
5KB
|
157 lines
Written by Brad Choate CServe ID: 75056,1247
The DEFAULT command.
#command DEFAULT <v1> WITH <x1> [, <vN> WITH <xN> ];
=> IF((<v1>)=NIL,<v1>:=<x1>,NIL) [; IF((<vN>)=NIL,<vN>:=<xN>,NIL)]
I've found this command useful for initializing variables that have
no value already.
The command
DEFAULT <variable> WITH <value>
is processed as ...
IF( <variable> = NIL , <variable> := <value> , NIL )
I use this command for every function I write that uses parameters.
This way, I can define default assignments for the parameter
variables.
FUNCTION ShowHeader(cString1,cString2)
DEFAULT cString1 WITH "", cString2 WITH ""
@ 0, 0
@ 0, 0 SAY cString1
@ 0,79-LEN(cString2) SAY cString2
RETURN NIL
In the above example, the function displays the first parameter in
the upper-left corner of the screen. The second parameter is
displayed in the upper-right corner of the screen. But... what
happens if...
ShowHeader("Program Name")
...and you *HAVEN'T* included the DEFAULT command? Well, most
likely, you'll get "Program Name" printed on the upper-left corner
of the screen, and then your program will crash when it tries to
get the length of cString2 since you cannot read the length of NIL.
When the DEFAULT command *IS* included, the function call
ShowHeader("Program Name")
does this: it displays "Program Name" on the upper-left corner
of the screen, and the displays a "" in the upper-right corner of the
screen. cString2="" because of the default command. Since cString2
equalled NIL upon entry into the function, the DEFAULT command declared
it as a null string since that was what it's default was declared as.
cString1 was not changed from the passed value since cString1 did not
equal NIL upon entry into the function.
The long way about doing this is ...
FUNCTION ShowHeader(cString1, cString2)
IF cString1=NIL
cString1=""
ENDIF
IF cString2=NIL
cString2=""
ENDIF
.
. // rest of the function as shown above
.
RETURN NIL
Now this works fine... but it involves a lot more effort. The
preprocessor converts the DEFAULT command to the equivalent of
the code shown above, so why go to all the trouble? Let the
preprocessor do it for you.
* * *
We must be careful not to create too many #commands, however.
Dozens of new commands, sprinkled throughout a program makes it
really hard to understand for others that don't know about the
language extensions that the original programmer developed.
However, the DEFAULT command is better suited as a command, and
not a function. This is because it needs to have the syntax of the
REPLACE command, the functionality of the STORE command, but the
brains to know when to store the value to the variable and when not
to.
Here are some more examples of using the DEFAULT command. These are
just to show how it works, and are not really practical uses.
cString2:="this is the second string"
cString4:="this is the first string"
DEFAULT cString1 WITH "String", cString2 WITH "String",;
cString3 WITH "String", cString4 WITH "String"
? cString1 // String
? cString2 // this is the second string
? cString3 // String
? cString4 // this is the first string
As you can see from this example, DEFAULT only assigns values to
variables that have no value already. cString1 and cString3 had
not been initialized, so DEFAULT initialized them with "String".
cString2 and cString4 retained their values because they did not
have NIL values. They were already initialized and had a value.
By the way, the default values can be anything you want. They do
not have to be character strings. And they do not have to be the same.
DEFAULT nDiscount WITH 10, cTerms WITH "NET 30", aItems WITH {},;
lDropShip WITH .F., aAddress WITH {}
..., etc. Here we have declarations for default data types of
numeric, character, logical, and arrays. You can also declare
defaults for any other data type including dates, and even code
blocks (but I wouldn't know why).
Well. It's now 2:30am, and I need my sleep. I'll upload this
tonight and I hope you have fun with it. It's my first command
and hopefully I won't get too carried away myself with writing
commands. I've only had Clipper 5.0 for two days, so this is
a pretty simple implementation of the #command directive, but
it really does suit the purpose it is intended for.
Enjoy.
Brad Choate (75056,1247)
Software Extensions
p.s. if any of this doesn't make any sense or you think that I
haven't explained this enough or that it is explained
wrong, then please pardon me. I'm very tired right now.
Send me a message via EasyPlex and I'll post an addendum
on the forum.
Software Extensions...
Specializing in Clipper programming for business (or other) applications.
Call us today for information on customized programming... (901) 274-8463.
ask for Barbara Oliver. Or mail your messages to CServe ID 75056,1247.